<?php

class Core_Backend_Component_Customer_Controller_Showroom extends Dcmp_Component_Controller {
	
	private $_poi = NULL;
	
	protected function initialize(){
		$poiid = $this->getRequest()->getParameter('poiid', NULL);
		if(!is_null($poiid)){
			$this->_poi = App::getModel('foundation/poi')->load($poiid);
			$this->getPage()->getNavigation()->addLabel($this->_poi->getShopName());
		}
	}

    public function indexAction($poiid, $pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
        $this->getHistory()->record();
        $list = App::getModel('customer/collection_showroom')
        				->joinAccount()
                        ->filterByPoiId($poiid)
                        ->pagination($pageindex, $pagesize)
                        ->load();
        $records = $list->loadCount();
        return $this->viewResult(array('list' => $list, 'records' => $records, 'poi' => $this->_poi));
    }

    public function createAction($poiid) {
        return $this->viewResult(array('poi' => $this->_poi));
    }

    public function editAction($id) {
        $model = App::getModel('customer/showroom')->load($id);
        $this->_poi = App::getModel('foundation/poi')->load($model->getPoiId());
        $this->getPage()->getNavigation()
        			->addLabel($this->_poi->getShopName())
        			->addLabel($model->getName());
        return $this->viewResult(array('poi' => $this->_poi, 'model'=>$model));
    }

    public function saveAction() {
        return $this->saveModel('draft');
    }
    
    public function submitAction() {
        return $this->saveModel('pending');
    }
    
    public function reviewAction($ids, $status, $statusmessage = ''){
        if($status == 'published'){
            App::getModel('customer/showroom')->publish($ids);
        }elseif($status == 'forbidden'){
            App::getModel('customer/showroom')->forbid($ids, $statusmessage);
        }
        return $this->historyResult('index', $this->__('share.message.success'));
    }
    
    public function deleteAction($ids){
    	App::getModel('customer/showroom')->deleteBatch($ids);
    	return $this->historyResult('index', $this->__('share.message.success'));    	
    }
    
    public function certificateIndexAction($id){
    	$this->getHistory()->record();
    	$showroom = App::getModel('customer/showroom')->load($id);
    	$certificates = $showroom->getCertificates()->joinAccount()->load();
    	$this->getPage()->getNavigation()->addLabel($showroom->getPoi()->getShopName())
    									 ->addLabel($showroom->getName());
		return $this->viewResult(array('showroom'=>$showroom, 'list'=>$certificates));
    }
    
    public function certificateGenerateAction($id) {
    	$showroom = App::getModel('customer/showroom')->load($id);
    	$this->getPage()->getNavigation()->addLabel($showroom->getPoi()->getShopName())
    									 ->addLabel($showroom->getName());
		return $this->viewResult(array('showroom'=>$showroom));
    }
    
    public function certificateGeneratePostAction() {
		$quantity = $this->getRequest()->getParameter('quantity');
		$data = $this->getRequest()->getParameter('model');
		print_r($data);
		for($i = 0; $i < $quantity; $i++){
			$model = App::getModel('customer/showroom_certificate');
			$model->addData($data)
				  ->setCode(rand(1000, 9999))
				  ->setStatus('published');
			$model->save();
		}
		return $this->historyResult('certificate-index', $this->__('share.message.success'));
    }
    
    public function certificateEditAction($id) {
    	$certificate = App::getModel('customer/showroom_certificate')->load($id);
    	$showroom = $certificate->getShowroom();
    	$this->getPage()->getNavigation()->addLabel($showroom->getPoi()->getShopName())
    									 ->addLabel($showroom->getName());
		return $this->viewResult(array('model'=>$certificate));
    }    

    public function certificateDeleteAction($ids) {
    	App::getModel('customer/showroom_certificate')->deleteBatch($ids);
    	return $this->historyResult('certificate-index', $this->__('share.message.success'));
    }      

    private function saveModel($status) {
        $data = $this->getRequest()->getForm('showroom');
        $model = App::getModel('customer/showroom');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setStatus($status);
        $model->setPreviewImage(basename($data['preview_image']));
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            Kd_Util_IO::moveFiles(Kd_Web_Util::urlToPath($data['preview_image']),
            					  DP_PUB.'customer'.DS.'showroom'.DS.$model->getId());
            return $this->historyResult('index', $this->__('share.message.success'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }
}